class FirstPage extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            list:[],
            thumburl: '',
            name: '',
            id: '0',
            selid:-1,
            partcount: 1,
            parts: [{src:''  }, {src:''  }, {src:'' }],
            categorylist: [],
            select_swiperlink:{name:'请选择'},
            swiperimglist: [],
            category_select_list: [],
            recommendtablehtml: [],
            recommendlist: [],
            goods_select_html: [],
            currentswipertype:0,
            swiperlinkhtml:[],
            swipertablehtml:[],
            swiperimghtml:[],
            swiper:{
                imgs:[],
                links:[]
            },
            entity:{
                name:"",
                id:''
            },
            fileimgshow:!0,
            imgstate:'',
            imgsrc:'',
            to:'',
        };
    }




    componentDidMount() {
        $('.modal1').hide()
       let cid = GetQueryString('id')
       this.setState({cid},()=>{
        this.freshlist()   
       })
            }
        freshlist(){
            dz_getsubcategorylist(this.state.cid,res=>{
               if(res.issuccess==1){
                this.setState({list:res.list,entity:res.entity||{name:"",
                id:''}})
              // this.formatlist(res.list);      
               console.log(res.list)
           }
        })

    }

 

/**新建 */
add(e) {
    $('.modal1').show()
//    var name = prompt('请输入二级分类名称');
//    if(name!=''){
//     aoedzsubcate({name,cid:this.state.cid},res=>{
//         if(res.issuccess='1'){
//             this.freshlist()  
//         }
//     })
//    }
}


    // 编辑
    edit(x) {
        var name =prompt('请编辑分类名称',x.name)
        console.log(name,'namename');
        if(name!=null){
            aoedzsubcate({id:x.id,name,cid:this.state.cid},res=>{
                if(res.issuccess==1){
                    this.freshlist()
                }else{
                    alert(res.msg)
                }
            })
        }
       
       // 
    }

    
    tosub(e){
        window.location.href = "./dz_subcate.html?id="+e;
    }
    // 排序
 
    // // 上架下架
    // setshow(e, i, cid) {
    //     let { start } = this.state
    //     Goodssetstate({ id: e, cid }, res => {
    //         if (res.issuccess == 1) {
    //             alert(`${i == "0" ? '下架' : '上架'}成功`)
    //             this.freshlist(start)
    //         }
    //     })
    // }
    // 删除
    del(e, cid) {
        this.setState({ delid: e, newcid: cid }, () => {
            $('#myModal').modal('toggle');
        })
    }
    confirmdel() {
        let { delid,start } = this.state
        dz_delsubcate( delid, res => {
            if (res.issuccess == 1) {
                alert('删除成功')
                $('#myModal').modal('toggle');
                this.freshlist(start)
            } else {
                $('#myModal').modal('toggle');
                alert(res.msg);
            }
        })
    }

    changeprice(id){
        var price = prompt('请输入单价');
        if(!isNaN(price)){
            dzsubcate_editprice({id,price},res=>{
                    if(res.issuccess==1){
                        this.freshlist()
                    }
            })
        }
    }

    changeunit(id){
        var unit = prompt('请输入单位');
        console.log(unit,'unit');
        if(unit.length>0){
            dzsubcate_editunit({id,unit},res=>{
                    if(res.issuccess==1){
                        this.freshlist()
                    }
            })
        }else{
            alert('请输入单位')
        }
    }

    



    closemodal(){
        $('.modal1').hide()
    }

    inputchange(prop, e) {

        this.setState({ [prop]: e.target.value })
    }
    confirm(){
        let{name,price,unit,selid} = this.state;
        if(isNaN(price)) {
            alert('价格输入错误')
          return;
        }

        aoedzsubcate({id:selid,name,cid:this.state.cid,unit},res=>{
            if(res.issuccess==1){
                this.freshlist()
            }else{
                alert(res.msg)
            }
        })

    }



    render() {
        var { list,entity,name,price,unit } = this.state;
        return (
            <div  className="breadLinebox">

<div className="modal1" >
                    <div onClick={this.closemodal.bind(this)} className="mask"></div>
                    <div className="wrap">
                        {/* <div  className="close-div">
                        <img onClick={this.closemodal.bind(this)} src="./src/close1.png" alt="" /> 
                    </div> */}

                        {/* <div className="content " > */}
                        {/* <div className="p1">选择应用</div>
                            <select value={select_app} onChange={this.selectfhchange.bind(this)}>
                                {
                                    applist.map((x, i) => {
                                        return (
                                            <option key={i} value={x.appId}>{x.appName} </option>
                                        )
                                    })
                                }
                            </select>
                        </div> */}
                        <div className="content">
                            <p className="p1">名称</p>
                            <input placeholder="请输入名称" allowClear value={name} onChange={this.inputchange.bind(this, 'name')} className="input" />
                            {/* <p className="p2">{zje}</p> */}
                        </div>
                        <div className="content">
                            <p className="p1">价格</p>
                            <input placeholder="请输入小标题" allowClear value={price} onChange={this.inputchange.bind(this, 'price')} className="input" />
                        </div>
                        <div className="content">
                            <p className="p1">单位</p>
                            <input placeholder="请输入小标题" allowClear value={unit} onChange={this.inputchange.bind(this, 'unit')} className="input" />
                        </div>
                        <div className="btns">
                            <p className="add" onClick={this.confirm.bind(this)}>确定</p>
                            <p className="add" onClick={this.closemodal.bind(this)}>取消</p>
                        </div>
                    </div>

                </div>


              <Modal content='确定要删除吗？' click={this.confirmdel.bind(this)} />
                <div className='content_wrap panel-default content_wrap'>
                    <div className='breadLine '>
                        当前位置 > 家具二级分类管理
                        </div>
                    {/* <div className='inputs panel-body'>
                        <h3>首页管理</h3>
                    </div> */}
                    <div className="content form">
                        <div>分类名称：{entity.name}</div>
                      
                        <div className="tabletit" >
                            {/* <p className="delall"><img src="./src/delall.png"/>批量删除</p> */}
                            <p className="add" onClick={this.add.bind(this)}>新建</p>
                        </div>
                        <table className="tab">
                            <thead>
                            <tr>
                            {/* <th className="minwidth tconter"><img src="./src/notselection.png"/></th> */}
                            <th className="tconter">序号</th>
                            <th className="tconter">名称</th>
                            <th className="tconter">单位</th>
                            <th className="tconter">单价</th>
                            <th className="cz tconter">操作</th>
                            </tr>
                            </thead>
                            <tbody>
                                {
                                    list.map((x,i)=>{
                                        return(
                                        <tr key={i}>
                                           {/* <td className="minwidth tconter"><img src="./src/notselection.png"/></td>  */}
                                           {/* <td className="tconter">{x.state=="0"?"显示":"隐藏"}</td>  */}
                                           <td className="tconter">{i+1}</td>
                                           <td className="tconter" onClick={this.edit.bind(this,x)}>{x.name}</td>
                                           <td onClick={this.changeunit.bind(this,x.id)} className="tconter">{x.unit}</td>
                                           <td onClick={this.changeprice.bind(this,x.id)} className="tconter">{x.price}</td>
                                           <td className="operatebox">
                                               <div className='operate' id={"ope"+x.id}>
                                                    {/* <div>
                                                       <img src={`./src/${x.state=="1"?"hide":"show"}.png`}/>
                                                       <p className='edit' onClick={this.setshow.bind(this,x.id,x.state)}>{x.state=="0"?"隐藏":"显示"}</p>
                                                   </div> */}
                                                   <div>
                                                       <img src="./src/del.png"/>
                                                       <p className='edit colred' onClick={this.del.bind(this,x.id)}>删除</p>
                                                   </div>
                                               </div>
                                            </td>
                                       </tr>
                                         )
                                    })
                                }

                            </tbody>
                        </table>
                   </div>
                
                </div >


            </div >
        )
    }
}





ReactDOM.render(
    <div>
        <TopLine />
        {/* <LeftMenu code='0' /> */}
        <LeftMenu code='dz_cate' />
        <FirstPage />
    </div>
    //<Barrage/>
    , $('.container')[0]
)